/**
 * Copyright (C) 2010, 2011 Neofonie GmbH
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package eu.dicodeproject.blogimport.parser;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;
import java.util.HashSet;
import java.util.Set;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


/**
 * A blacklist that will be used in the generic parser when
 * parsing the /Document/Content nodes. XPathes will converted to the defined
 * format like "/" will replace with "_", the first "/" will remove. Example:
 * /Document/Content/Title will transform to
 * Document_Content_Title. Thats the internal format of document store
 * xpathes
 *  
 * 
 */

// TODO: check if we want to use a whitelist instead
public class XPathBlacklist {
  
	/** Logger for this class. */
	private static final Logger LOG = LoggerFactory.getLogger(XPathBlacklist.class);
	
	/**
   * Set that contains blacklisted xpaths.
   */
  protected Set<String> blacklist = new HashSet<String>();


	{
		if (null != XPathBlacklist.class.getClassLoader().getResource("blacklist.txt")) {
			URL stream = XPathBlacklist.class.getClassLoader().getResource("blacklist.txt");
			BufferedReader reader = null;
			try {
				reader = new BufferedReader(new InputStreamReader(stream.openStream()));
				String line = null;
				LOG.info("Loading classpath resource blacklist.txt.");
				while ((line = reader.readLine()) != null) {
					line = line.substring(1).replaceAll("/", "_").trim();
					blacklist.add(line);
				}
				reader.close();
			} catch (IOException e) {
				LOG.warn("Cannot read classpath resource blacklist.txt. Using the in class defined xpathes.", e);
			} finally {
				if (reader != null) {
					try {
						reader.close();
					} catch (IOException e) {
						LOG.error("Cannot close reader to file " + stream.getFile());
					}
				}
			}
		 
		}
		LOG.info("Defined and converted blacklisted xpathes=" + blacklist);
	}
	 /**
   * This method checks if the referenced xpath is blacklisted. If it is, the
   * method will return true and the content of the xpath shall not be parsed
   * from the generic parser.
   * 
   * @param xpath
   *            the xpath of a node as {@link String}
   * @return true if the xpath is blacklisted, false if not
   */
  // TODO: add a check for empty lines!
  public boolean isBlacklisted(final String xpath) {
    boolean blacklisted = blacklist.contains(xpath);
    if (!blacklisted) {      
      for (String blentry : blacklist) {
        blacklisted = xpath.startsWith(blentry);
        if (blacklisted) {
          break;
        }
      }
    }
    return blacklisted;
  }
}
